package com.lzt.app

import org.json4s.{DefaultFormats, Formats, JValue}
import org.scalatra.ScalatraServlet
import org.scalatra.json.JacksonJsonSupport
import org.slf4j.{Logger, LoggerFactory}
import org.json4s.JsonDSL._

class ServerServlet extends ScalatraServlet with JacksonJsonSupport {

  private val logger: Logger = LoggerFactory.getLogger(classOf[ServerServlet])
  protected implicit lazy val jsonFormats: Formats = DefaultFormats

  before() {
    contentType = formats("json")
  }

  get("/tps") {
    val date = request.getParameter("date")
    val service = request.getParameter("service")
    logger.debug("request date:%s, service:%s".format(date, service))

    var args: Tuple2[String, String] = null
    if (StringUtil.isNull(date)) {
      args = StringUtil.getLastMinute()
    } else {
      args = StringUtil.getTodayMinute(StringUtil.parseDate(date))
    }

    import HbaseTemplate.convertJson

    val result = HbaseTemplate.scan[JValue]("service_tpm", s"${args._1}-${service}-tpm", s"${args._2}-${service}-tpm")
    logger.debug("当天 %s 系统 tpm：{}".format(service), result)

    compact(result)
  }
}